package com.zzx.test.question;

import java.util.HashMap;
import java.util.Map;

public class LeetCode2 {

    public static void main(String[] args) {
//        System.out.println("加一:" + Arrays.toString(plusOne(new int[]{9})));
//        System.out.println("验证回文串:" + isPalindrome("0p"));
//        System.out.println("只出现一次的数字:" + singleNumber(new int[]{4, 1, 1, 2, 2}));
//        int[] ints = {1, 2, 3, 4, 5, 6, 7};
//        rotate(ints, 8);
//        System.out.println("轮转数组:" + Arrays.toString(ints));
//        System.out.println("有效的字母异位词:" + isAnagram("aa", "bb"));
//        int[] ints = {1,0,0,3,12};
//        moveZeroes(ints);
//        System.out.println("移动零:" + Arrays.toString(ints));
//        char[] chars = new char[]{'h', 'e', 'l', 'l', 'o'};
//        reverseString(chars);
//        System.out.println("反转字符串:" + Arrays.toString(chars));
//        System.out.println("字符串中的第一个唯一字符:"+firstUniqChar("aabb"));
//        System.out.println("最大连续 1 的个数:" + findMaxConsecutiveOnes(new int[]{1, 0, 1, 1, 0, 1}));
        System.out.println("宝石与石头:" + numJewelsInStones("z", "ZZ"));
    }

    public static int[] plusOne(int[] digits) {
        for (int i = digits.length - 1; i >= 0; i--) {
            if (digits[i] == 9 && i != 0) {
                digits[i] = 0;
            } else if (digits[i] == 9 && i == 0) {
                int[] res = new int[digits.length + 1];
                res[0] = 1;
                return res;
            } else {
                digits[i] += 1;
                return digits;
            }
        }
        return digits;
    }

    public static boolean isPalindrome(String s) {
        boolean flag = true;
        StringBuilder stringBuilder = new StringBuilder();
        for (char c : s.toCharArray()) {
            if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
                stringBuilder.append(c);
            } else if (c >= 'A' && c <= 'Z') {
                stringBuilder.append(Character.toLowerCase(c));
            }
        }
        String string = stringBuilder.toString();
        int left = 0;
        int right = string.length() - 1;
        while (left < right) {
            if (string.charAt(left++) != string.charAt(right--)) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static int singleNumber(int[] nums) {
        if (nums.length == 1) return nums[0];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num)) {
                map.put(num, map.get(num) + 1);
                continue;
            }
            map.put(num, 1);
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                return entry.getKey();
            }
        }
        return 0;
    }

    public static void rotate(int[] nums, int k) {
        if (nums.length == 1) {
            return;
        } else if (k > nums.length) {
            k %= nums.length;
        }
        for (int i = 0; i < k; i++) {
            int tmp = 0;
            for (int j = nums.length - 1; j >= 0; j--) {
                if (j == nums.length - 1) {
                    tmp = nums[j];
                    continue;
                }
                nums[j + 1] = nums[j];
            }
            nums[0] = tmp;
        }
    }

    public static boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] ints = new int[26];
        char[] sCharArray = s.toCharArray();
        char[] tCharArray = t.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            ints[sCharArray[i] - 'a']++;
            ints[tCharArray[i] - 'a']--;
        }
        for (int i : ints) {
            if (i != 0) {
                return false;
            }
        }
        return true;
    }

    public static void moveZeroes(int[] nums) {
        int left = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != 0 && nums[left] == 0) {
                nums[left] = nums[i];
                nums[i] = 0;
            }
            if (nums[left] != 0) {
                left++;
            }
        }
    }

    public static void reverseString(char[] s) {
        int left = 0, right = s.length - 1;
        while (left < right) {
            char tmp = s[right];
            s[right--] = s[left];
            s[left++] = tmp;
        }
    }

    public static int firstUniqChar(String s) {
        if (s.length() == 1) {
            return 0;
        }
        int[] ints = new int[26];
        char[] charArray = s.toCharArray();
        for (char c : charArray) {
            ints[c - 'a']++;
        }
        for (int i = 0; i < charArray.length; i++) {
            if (ints[charArray[i] - 'a'] == 1) {
                return i;
            }
        }
        return -1;
    }

    public static int findMaxConsecutiveOnes(int[] nums) {
        int res = 0, cur = 0;
        for (int num : nums) {
            if (num == 1) {
                res = Math.max(res, ++cur);
            } else {
                cur = 0;
            }
        }
        return res;
    }

    public static int numJewelsInStones(String jewels, String stones) {
        int res = 0;
        char[] charArray1 = jewels.toCharArray();
        char[] charArray2 = stones.toCharArray();
        for (char c : charArray1) {
            for (char c1 : charArray2) {
                if (c1 == c) {
                    res++;
                }
            }
        }
        return res;
    }

}
